package gxprod.androidai.widgets;

import gxprod.androidai.R;
import gxprod.androidai.fetch.FetchWeather.WeatherCondition;
import gxprod.androidai.fetch.FetchWeather.WeatherModel;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class WeatherWidget extends Widget{

	public static final int DAYS = 5;

	protected LinearLayout topLevelView;
	protected LayoutInflater inflater;
	protected View root;

	public ImageView[] Images;
	public TextView[] Conditions;
	public TextView[] Highs;
	public TextView[] Lows;

	private int[] ImageIds = new int[] { R.id.ImageView1, R.id.ImageView2, R.id.ImageView3, R.id.ImageView4, R.id.imageView5 };
	private int[] ConditionIds  = new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5 };
	private int[] HighIds   = new int[] { R.id.textView6, R.id.textView7, R.id.textView8, R.id.textView9, R.id.textView10 };
	private int[] LowIds   = new int[] { R.id.textView11, R.id.textView12, R.id.textView13, R.id.textView14, R.id.textView15 };

	public WeatherWidget(final Activity context, ViewFlipper switcher) {
		super(context);
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		context.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				root = inflater.inflate(R.layout.weather_large, null);

				Images		= new ImageView[DAYS];
				Conditions	= new TextView[DAYS];
				Highs		= new TextView[DAYS];
				Lows		= new TextView[DAYS];
				for(int i = 0; i < DAYS; i++) {
					Images[i]		= (ImageView) root.findViewById(ImageIds[i]);
					Conditions[i]	= (TextView) root.findViewById(ConditionIds[i]);
					Highs[i]		= (TextView) root.findViewById(HighIds[i]);
					Lows[i]			= (TextView) root.findViewById(LowIds[i]);
				}
			}
		});
	}
	
	public void populate(WeatherModel model) {
		for(int i = 0; i < DAYS; i++) {
			WeatherCondition wc = model.Later.get(i);			
			Images[i].setImageResource(getConditionImageId(wc));			
			Conditions[i].setText(wc.Descript);
			
			int high = wc.getHigh();
			int low = wc.getLow();
			Highs[i].setText("" + high);
			Lows[i].setText("" + low);
			if(high <= low) {
				Highs[i].setText(" ? ");
				Lows[i].setText(" ? ");
			}
		}
	}

	private int getConditionImageId(WeatherCondition condition) {
		int imgId = R.drawable.na;
		String desc = condition.Descript.toLowerCase();
		if(desc.contains("rain")) {
			imgId = R.drawable.rain;
		} else if(desc.contains("snow")) {
			imgId = R.drawable.snow;
		} else if(desc.contains("cloud")) {
			if(desc.equals("Partly Cloudy"))
				imgId = R.drawable.partialy_cloudy;
			else if(desc.equals("Cloudy"))
				imgId = R.drawable.cloudy;
		} else if(desc.contains("clear") | desc.contains("sunny"))
			imgId = R.drawable.sunny;
		return imgId;
	}

	@Override
	public View getTopLevelView() {
		return root;
	}
}
